<?php

/***
 * Class IndexController
 * 后台首页，包括登陆功能
 */
class IndexController extends ManageController{

    protected $_admin_user_model = null;

    function init(){
        parent::init();
    }

    #主页
    public function indexAction(){
        $this->_layout->meta_title = '首页';
    }

    #登陆
    public function loginAction(){
        $redirect_url = isset($_GET['redirect_url']) ? addslashes(htmlspecialchars(trim($_GET['redirect_url']))): '';

        if($this->getRequest()->isPost()) {
            $email = addslashes(htmlspecialchars(trim($_POST['email'])));
            $password = addslashes(htmlspecialchars(trim($_POST['password'])));

            if(empty($email) || empty($password)) {
                $this->_view->message = "邮箱和密码不能为空!";
            } else {
                $admin_user_model = new AdminUserModel();
                try {
                    $row = $admin_user_model->login($email,$password);
                }catch(Exception $ex){
                    $this->_view->message = $ex->getMessage();
                }
                if(empty($row)){
                    $this->set_popup_message('邮箱和密码不对');
                    $this->redirect('/manage/index/login/');
                    exit;
                }

                $user = array('email' => '','uid'=> '','username' => '', 'status' => '', 'groupid' => 0, 'limit_modify'=>0);
                foreach ($user as $key => $value) {
                    $user[$key] = $row[$key];
                }
                $this->set_current_user($user);

                #登陆后跳转之前的链接网址
                if(empty($redirect_url) || $redirect_url == '/manage/index/login'){
                    $this->redirect('/manage/index/index/');
                }else{
                    $this->redirect($redirect_url);
                }
                exit;
            }
        }

        $this->_view->redirect_url = $redirect_url;
        $this->_layout->meta_title = '登录';
    }

    #退出登录
    public function logoutAction(){
        $this->set_current_user(array());
        $this->redirect('/admin/index/index/');
        return FALSE;
    }

}
?>